package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.dubbo.mappers.SmsFlashPromotionMpper;
import com.mall.dubbo.mappers.SmsFlashPromotionProductRelationMapper;
import com.tanhua.model.domain.SmsFlashPromotion;
import com.tanhua.model.domain.SmsFlashPromotionProductRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService
public class SmsFlashPromotionApiImpl implements SmsFlashPromotionApi{
    @Autowired
    private SmsFlashPromotionMpper smsFlashPromotionMpper;

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    /**
     *通过Title 判断有没有同名id
     */
    public void getByTitle(SmsFlashPromotion smsFlashPromotion){
        if (smsFlashPromotion != null  || smsFlashPromotion.getTitle() != null){
            LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SmsFlashPromotion::getTitle,smsFlashPromotion.getTitle());
            SmsFlashPromotion smsFlashPromotion1 = smsFlashPromotionMpper.selectOne(lqw);
            if (smsFlashPromotion1 != null){
                throw new BusinessException("活动已经存在");
            }
        }


    }

    /**
     * 查询秒杀活动
     * @param keyword
     * @param page
     * @return
     */
    @Override
    public Page flashList(String keyword, Page<SmsFlashPromotion> page) {
        LambdaQueryWrapper<SmsFlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null,SmsFlashPromotion::getTitle,keyword);
        return smsFlashPromotionMpper.selectPage(page,lqw);
    }
    /**
     * 修改秒杀上下线
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {

        SmsFlashPromotion smsFlashPromotion = new SmsFlashPromotion();
        smsFlashPromotion.setId(id);
        smsFlashPromotion.setStatus(status);
        this.getByTitle(smsFlashPromotion);
        smsFlashPromotionMpper.updateById(smsFlashPromotion);
    }

    /**
     * 添加秒杀活动
     * @param smsFlashPromotion
     * @return
     */
    @Override
    public void create(SmsFlashPromotion smsFlashPromotion) {
        this.getByTitle(smsFlashPromotion);
        smsFlashPromotion.setCreateTime(new Date());
        smsFlashPromotionMpper.insert(smsFlashPromotion);

    }
    /**
     * 编辑秒杀活动
     * @param smsFlashPromotion
     */
    @Override
    public void update(SmsFlashPromotion smsFlashPromotion) {
        smsFlashPromotionMpper.updateById(smsFlashPromotion);
    }

    /**
     * 删除活动
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        smsFlashPromotionMpper.deleteById(id);

        //删除sms_flash_promotion_product_relation表中数据
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id != null,SmsFlashPromotionProductRelation::getFlashPromotionId,id);
        smsFlashPromotionProductRelationMapper.delete(lqw);
    }


}
